﻿using MyControlClassLibrary;
using MyControlClassLibrary.Node;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;//引用命名

namespace MotionGTS
{
    public partial class Frm_Coordinates : DockContent
    {
        #region 外部需要调用这个类的方法使用静态委托对象来访问

        /// <summary>
        /// 运动方向
        /// </summary>
        public static  Direction direction = Direction.None;
        /// <summary>
        /// 步长
        /// </summary>
        int Setp = 2;

        /// <summary>
        /// 设置坐标点的委托
        /// </summary>

        public static Action<int,int> acMovePoint=null;

        /// <summary>
        /// 设置坐标点X的委托
        /// </summary>
        public static Action<int> acMovePointX = null;
        /// <summary>
        /// 设置坐标点Y的委托
        /// </summary>
        public static Action<int> acMovePointY = null;


        /// <summary>
        /// 设置绘图模式的委托
        /// </summary>
        public static Action<DrawingMode> acSetDrawingMode = null;

        /// <summary>
        /// 创建九点坐标点的委托
        /// </summary>
        public static Action acCreatePointMarkers = null;

        /// <summary>
        /// 开始动画的委托
        /// </summary>
        public static Action acStartAnimation = null;
        /// <summary>
        /// 停止动画的委托
        /// </summary>
        public static Action acStopAnimation = null;

        #endregion

        public Frm_Coordinates()
        {
            InitializeComponent();
            acMovePoint = new Action<int, int>(SetPoint);
            acMovePointX = new Action<int>(SetPointX);
            acMovePointY = new Action<int>(SetPointY);
            acSetDrawingMode = new Action<DrawingMode>(this.canvasControl1.SetDrawingMode);
            acCreatePointMarkers = new Action(CreatePointMarkers);
            //设置更新形状后列表的操作
            this.canvasControl1.acUpdateListBox += Frm_GDI.acUpdateListBox;
            //设置开始动画
            acStartAnimation = new Action(this.canvasControl1.StartAnimation);
            //设置停止动画
            acStopAnimation = new Action(this.canvasControl1.StopAnimation);
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (direction)
            {
                case Direction.None:
                    break;
                case Direction.Up:
                    this.canvasControl1.MovePointY += Setp;
                    break;
                case Direction.Down:
                    this.canvasControl1.MovePointY -= Setp;
                    break;
                case Direction.Left:
                    this.canvasControl1.MovePointX -= Setp;
                    break;
                case Direction.Right:
                    this.canvasControl1.MovePointX += Setp;
                    break;
            }
        }

        /// <summary>
        /// 设置机械点坐标点
        /// </summary>
        /// <param name="x">像素坐标X</param>
        /// <param name="y">像素坐标Y</param>
        public void SetPoint(int x, int y)
        {
            this.canvasControl1.MovePointX = x;
            this.canvasControl1.MovePointY = y;
        }

        public void SetPointX(int x)
        {
            this.canvasControl1.MovePointX = x;
        }

        public void SetPointY(int y)
        {
            this.canvasControl1.MovePointY = y;
        }

        //创建九个坐标点与轨迹
        public void CreatePointMarkers()
        {
            //清空容器中的所有坐标点
            this.canvasControl1.NodeList.Clear();

            //定义坐标点的间隔
            int spacing = 80;//相邻点之间的间隔

            //创建九个坐标点，按照九宫格的方式进行排列
            NodeClass node1 = new NodeClass("1号点", -spacing, spacing, Color.Red);
            NodeClass node2 = new NodeClass("2号点", 0, spacing, Color.Red);
            NodeClass node3 = new NodeClass("3号点", spacing, spacing, Color.Red);
            NodeClass node4 = new NodeClass("4号点", -spacing, 0, Color.Red);
            NodeClass node5 = new NodeClass("5号点", 0, 0, Color.Red);
            NodeClass node6 = new NodeClass("6号点", spacing, 0, Color.Red);
            NodeClass node7 = new NodeClass("7号点", -spacing, -spacing, Color.Red);
            NodeClass node8 = new NodeClass("8号点", 0, -spacing, Color.Red);
            NodeClass node9 = new NodeClass("9号点", spacing, -spacing, Color.Red);

            //添加到集合中
            this.canvasControl1.NodeList.Add(node1);
            this.canvasControl1.NodeList.Add(node2);
            this.canvasControl1.NodeList.Add(node3);
            this.canvasControl1.NodeList.Add(node4);
            this.canvasControl1.NodeList.Add(node5);
            this.canvasControl1.NodeList.Add(node6);
            this.canvasControl1.NodeList.Add(node7);
            this.canvasControl1.NodeList.Add(node8);
            this.canvasControl1.NodeList.Add(node9);

            //创建形状对象
            CustomPathShape customPathShape = new CustomPathShape("九点坐标轨迹路径", Color.Red);
            //由于是四象限的坐标系，需要进行坐标点转换=>像素的中心点
            Point ZeroPoint = new Point(this.canvasControl1.Width / 2, this.canvasControl1.Height / 2);
            //将节点坐标添加到轨迹路径中,而节点坐标存储在控件中，对控件的NodeClass节点进行遍历取出当中的坐标
            foreach (NodeClass item in this.canvasControl1.NodeList)
            {
                //进行坐标点转换，坐标轴的坐标转换为像素坐标
                Point shapePoint = new Point(ZeroPoint.X + item.X, ZeroPoint.Y - item.Y);
                //转换后的坐标保存进入到自定义形状中
                customPathShape.Upadate(shapePoint);
            }
            //添加到形状集合中
            this.canvasControl1.ShapeList.Add(customPathShape);
            //添加到形状列表中
            Frm_GDI.acUpdateListBox(this.canvasControl1.ShapeList);
            //刷新控件显示
            this.canvasControl1.Invalidate();//强制刷新
        }

    }
}
